;;; GNU Guix --- Functional package management for GNU
;;;
;;; Copyright © 2021 Raghav Gururajan <rg@raghavgururajan.name>
;;;
;;; This file is not part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (guixrus packages scintilla)
  #:use-module (guix build-system gnu)
  #:use-module (guix download)
  #:use-module (guix git-download)
  #:use-module (guix packages)
  #:use-module (guix utils)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (gnu packages)
  #:use-module (gnu packages base)
  #:use-module (gnu packages gcc)
  #:use-module (gnu packages gtk)
  #:use-module (gnu packages lua)
  #:use-module (gnu packages ncurses)
  #:use-module (gnu packages pkg-config)
  #:use-module (gnu packages python)
  #:use-module (gnu packages text-editors))

(define-public lexilla
  (package
    (name "lexilla")
    (version "5.1.1")
    (source
     (origin
       (method git-fetch)
       (uri
        (git-reference
         (url "https://github.com/ScintillaOrg/lexilla")
         (commit (string-append "rel-5-1-1"))))
       (file-name (git-file-name name version))
       (sha256
        (base32 "07jmamrflm628f47r76z7dvmk79mw1w753g6g75jl0qa8g5v9c22"))))
    (build-system gnu-build-system)
    (arguments
     `(#:imported-modules
       ((guix build copy-build-system)
        ,@%gnu-build-system-modules)
       #:modules
       (((guix build copy-build-system)
         #:prefix copy:)
        (guix build gnu-build-system)
        (guix build utils))
       #:phases
       (modify-phases %standard-phases
         (add-after 'unpack 'patch
           (lambda* (#:key inputs #:allow-other-keys)
             (substitute* (find-files "." ".*mak.*")
               (("\\.\\./\\.\\./scintilla/include")
                (string-append (assoc-ref inputs "scintilla")
                               "/include")))))
         (delete 'configure)
         (replace 'build
           (lambda _
             (with-directory-excursion "src"
               (invoke "make" "all"))))
         (replace 'check
           (lambda* (#:key tests? #:allow-other-keys)
             (when tests?
               (with-directory-excursion "test"
                 (invoke "make" "test")))))
         (replace 'install
           (lambda args
             (apply (assoc-ref copy:%standard-phases 'install)
                    #:install-plan
                    '(("bin" "lib"
                       #:include-regexp ("\\.so$"))
                      ("include" "include/lexilla"
                       #:include-regexp ("\\.h$"))
                      ("access" "include/lexilla/access"
                       #:include-regexp ("\\.h$"))
                      ("lexlib" "include/lexilla/lexlib"
                       #:include-regexp ("\\.h$"))
                      ("doc" "share/doc/lexilla"
                       #:include-regexp ("\\.(html|png)$")))
                    args))))))
    (native-inputs
     (list
      gcc-9
      pkg-config
      python))
    (inputs
     (list
      scintilla))
    (home-page "https://www.scintilla.org/Lexilla.html")
    (synopsis "Library of language lexers for use with Scintilla")
    (description "Lexilla is a free library of language lexers that can be used
with the Scintilla editing component.")
    (license license:hpnd)))

(define-public scinterm
  (package
    (name "scinterm")
    (version "3.1")
    (source
     (origin
       (method git-fetch)
       (uri
        (git-reference
         (url "https://github.com/orbitalquark/scinterm")
         (commit (string-append "scinterm_" version))))
       (file-name (git-file-name name version))
       (sha256
        (base32 "1a59fqrb6gjwy67v6jbfcjh9bzmndcbz13l8sh4pjy77ncja45kl"))))
    (build-system gnu-build-system)
    (arguments
     `(#:tests? #f                      ;no test-suite
       #:phases
       (modify-phases %standard-phases
         (add-after 'unpack 'copy-inputs
           (lambda* (#:key inputs #:allow-other-keys)
             (let ((dest (string-append (getenv "TMP") "/source"))
                   (src (assoc-ref inputs "scintilla")))
               (invoke "tar" "zxvf" src "-C" dest))
             (copy-recursively "scintilla" "../")))
         (add-after 'copy-inputs 'patch
           (lambda* (#:key inputs #:allow-other-keys)
             (substitute* "Makefile"
               (("scintilla.a") "scinterm.a"))))
         (delete 'configure)
         (replace 'build
           (lambda _
             (invoke "make" "patch")
             (invoke "make" "all")))
         (replace 'install
           (lambda* (#:key outputs #:allow-other-keys)
             (install-file
              "../bin/scinterm.a"
              (string-append (assoc-ref outputs "out") "/lib"))
             (install-file
              "ScintillaCurses.h"
              (string-append (assoc-ref outputs "out") "/include/scinterm")))))))
    (native-inputs
     `(("lua" ,lua)
       ("pkg-config" ,pkg-config)
       ("python" ,python-wrapper)
       ("scintilla"
        ,(origin
           (method url-fetch)
           (uri "https://www.scintilla.org/scintilla511.tgz")
           (sha256
            (base32 "1d0yjx2wlx4fj5bccxdgfmrr7nzazkw4m08i6h4c0a54sb484yif"))))))
    (inputs
     (list
      ncurses))
    (home-page "https://orbitalquark.github.io/scinterm/")
    (synopsis "Curses platform for Scintilla")
    (description "Scinterm is a curses platform for Scintilla that supports ncurses,
PDCurses, and X/Open Curses.")
    (license license:expat)))

